package com.hexinfo.dmpro.component.comparison.service.impl;

import cn.hutool.core.date.DateUtil;
import com.hexinfo.dmpro.component.common.exception.BizException;
import com.hexinfo.dmpro.component.comparison.entity.TableContrastDataManagementEntity;
import com.hexinfo.dmpro.component.comparison.entity.TableContrastParamEntity;
import com.hexinfo.dmpro.component.comparison.entity.TableContrastResultEntity;
import com.hexinfo.dmpro.component.comparison.handler.ITableContrastHandler;
import com.hexinfo.dmpro.component.comparison.service.ITableContrastService;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Service
public class TableContrastService implements ITableContrastService {

    @Resource
    private TableContrastDataManagementEntity dataManagement;

    @Resource
    private ITableContrastHandler tableContrastHandler;

    /**
     * @Method createTableContrastTask
     * @Param tableContrastParam
     * @Return boolean
     * @Description 增加表比对任务
     * @Author Huang Yanyihe
     * @Date 2023/9/10 23:01
     * @Version V1.0
     */
    @Override
    public boolean createTableContrastTask(TableContrastParamEntity tableContrastParam) {
        log.info("[数据比对组件]- 表[{}]数据对比任务创建,创建时间:{}", tableContrastParam.getTableFlag(), DateUtil.now());
        try {
            // ① 检查当前表是否正在比对
            checkTableIsContrast(tableContrastParam);
            // ② 构建表对比结果对象
            buildTableContrastResultEntity(tableContrastParam);
            // ③ 开启表数据对比任务
            tableContrastHandler.startTableDataContrast(tableContrastParam);
            return true;
        } catch (BizException e) {
            log.warn("[数据比对组件]- 表[{}]数据对比任务创建失败,失败原因: {}", tableContrastParam.getTableFlag(), e.getMessage());
        } catch (Exception e) {
            log.error("[数据比对组件]- 表[{}]数据对比任务创建失败,失败原因: {}", tableContrastParam.getTableFlag(), e.getMessage(), e);
        } finally {
            log.info("[数据比对组件]- 表[{}]数据对比任务创建, 创建完成时间:{}", tableContrastParam.getTableFlag(), DateUtil.now());
        }
        return false;
    }

    /**
     * @Method queryTableContrastResult
     * @Param tableContrastParam
     * @Return com.hexinfo.dmpro.component.comparison.entity.TableContrastResultEntity
     * @Description 查询表比对任务结果集
     * @Author Huang Yanyihe
     * @Date 2023/9/10 23:02
     * @Version V1.0
     */
    @Override
    public TableContrastResultEntity queryTableContrastResult(@NonNull String tableFlag) {
        return dataManagement.getStatusMap().get(tableFlag);
    }

    @Override
    public Map<String, List<TableContrastResultEntity>> queryAllTableContrastResult() {
        Map<String, List<TableContrastResultEntity>> resultMap = new HashMap();
        for (TableContrastResultEntity tableContrastResultEntity : dataManagement.getStatusMap().values().stream().collect(Collectors.toList())) {
            List<TableContrastResultEntity> constrastStatusList = new ArrayList<>();
            if (resultMap.containsKey(tableContrastResultEntity.getStatus())) {
                constrastStatusList = resultMap.get(tableContrastResultEntity.getStatus());
            }
            constrastStatusList.add(tableContrastResultEntity);
            resultMap.put(tableContrastResultEntity.getStatus(), constrastStatusList);
        }
        return resultMap;

    }

    /**
     * @Method cancelTableContrastTask
     * @Param tableFlag
     * @Return boolean
     * @Description 终止表比对任务
     * @Author Huang Yanyihe
     * @Date 2023/9/18 17:09
     * @Version V1.0
     */
    @Override
    public boolean cancelTableContrastTask(String tableFlag) {
        try {
            tableContrastHandler.cancelTableContrastTask(tableFlag);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * @Method: checkTableIsContrast
     * @Param: [tableContrastParam]
     * @Return: void
     * @Description：判断当前表是否正在比对<br>
     * @Author： wz.li
     * @Date： 2023/9/16 14:08
     * @Version： V2.0.2
     */
    private void checkTableIsContrast(TableContrastParamEntity tableContrastParam) {
        if (dataManagement.contrastIsRunning(tableContrastParam.getTableFlag())) {
            throw new BizException("任务已经存在,请等待!");
        }
    }

    /**
     * @Method buildTableContrastResultEntity
     * @Param tableContrastParam
     * @Return void
     * @Description 构建表比对结果对象
     * @Author Huang Yanyihe
     * @Date 2023/9/18 17:19
     * @Version V1.0
     */
    private void buildTableContrastResultEntity(TableContrastParamEntity tableContrastParam) {
        dataManagement.getStatusMap().put(tableContrastParam.getTableFlag(), new TableContrastResultEntity(tableContrastParam));
    }
}
